﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;

namespace ADECMS.UserPages
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class FileDownloadImageHandle : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            String filename = context.Request.QueryString["fileName"];
            try
            {
                string path = HttpContext.Current.Server.MapPath("/Images/FileDownload/");
                string filepath = HttpContext.Current.Server.MapPath("/Uploads/Files/" + filename);
                string name = Path.GetFileName(filepath);
                string ext = Path.GetExtension(filepath);
                if (ext != null)
                {
                    switch (ext.ToLower())
                    {
                        case ".htm":
                        case ".html":
                            path += "html.jpg";
                            break;

                        case ".txt":
                        case ".doc":
                        case ".docx":
                        case ".rtf":
                            path += "word.jpg";
                            break;
                        case ".jpg":
                        case ".png":
                            path += "jpg.jpg";
                            break;
                        case ".pdf":
                            path += "pdf.jpg";
                            break;
                        default:
                            path += "unknown.jpg";
                            break;
                    }
                }else
                {
                    path += "unknown.jpg";
                }


                FileStream fs = File.OpenRead(path);
                byte[] slika = new byte[fs.Length];
                fs.Read(slika, 0, slika.Length);
                context.Response.ContentType = "image/jpeg";
                context.Response.BinaryWrite(slika);
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
